package cn.itcast.store.domain;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class Cart {
	
	//总计/积分
	private double total = 0;
	
	//个数不确定的购物项 商品pid<===>CartItem
	Map<String,Cartlterm> map=new HashMap<String,Cartlterm>();
	
	
	
	public double getTotal() {
		total =0;
		
		Collection<Cartlterm>  Cartlterms=map.values();
		for (Cartlterm cartlterm : Cartlterms) {
			total+= total=cartlterm.getSubtotal();
			
		}
		
		return total;
		
	}
	
	public void setTotal(int total) {
		this.total = total;
	}
	public Map<String, Cartlterm> getMap() {
		return map;
	}
	public void setMap(Map<String, Cartlterm> map) {
		this.map = map;
	}
	
	//返回MAP中所有的值
	public Collection<Cartlterm> getCartItems(){
		
		return map.values();
	}
	
	
	//添加购物项到购物车
	
	public void addCartItemToCar(Cartlterm cartItem){
		System.out.println("111111111");
		
		
		String pid=cartItem.getProduct().getPid();
		//String  pid =cartItem.getProduct().getPid();
		
		//System.out.println("111111111");
		
		if(map.containsKey(pid)){
			Cartlterm oldItem=map.get(pid);
			oldItem.setNum(oldItem.getNum()+cartItem.getNum());
		}else{
			map.put(pid, cartItem);
		}
		
		
		
	}
	
	//删除购物项
	public void removeCartItem(String pid){
		map.remove(pid);
	}
	
	//清空购物车
	public void clearCart(){
		map.clear();
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
